<?php

namespace Home\Controller;


/**
* 商户控制器
*
*/
class ShopController extends HomeController {

    /**
     * 商户入驻
     */

    public function add()
    {
        $uid = I('get.uid');

        if (IS_POST) {
            $model = D('Kbxx');
            $isAddKbxx = $model->where('uid='.$uid)->count();

            if(!$isAddKbxx){
                //未添加商户信息
                $this->showError('请联系管理员开通权限');

            }

            $params = I('post.');
            $params['uid'] = $uid;

            if (empty($_POST['name'])) {
                $this->showError('请填写姓名');
            }

            if($params['rate2'] > $params['rate']){
                $this->showError('费率不可高于平台费率');
            }

            if($params['rate2'] < 0.6){
                $this->showError('费率不可低于0.6');
            }

            $res = M('kbsh')->add($params);

            if($res !== false){


                $this->showSuccess();
            }else{
                $this->showError('入驻失败');
            }
        }

    }

    /**
     * 商户授权码url地址
     */
    public function getQrcodeUrl(){
        $uid = I('uid');
        $kbsh_id = I('kbsh_id'); //码商id

        $kbsh = M('kbsh')->find($kbsh_id);

        $one = D('Kbxx')->where('uid=' . $uid)->find();
        if (empty($one)) {
            $this->showError('没有这个商户 ');
        } else {

            if (empty($one['name'])) {
                //有这个商户  但是没有上传身份证信息
                $this->showError('商户信息不完整');
            } else if (!empty($one['name']) && $one['real_status'] == 0) {
                // 有这个商户 已上传身份证 但未通过验证
                $this->showError('您的认证信息已上传，请耐心等候人工审核~');
            }
        }

//        $adata=D('Adata')->where(['id'=>1])->find();
        $adata = M('shouquan')->find();
        $appid=$adata['appid'];

        //&kbsh_id=$kbsh_id
//        $url = "https://openauth.alipay.com/oauth2/appToAppAuth.htm?app_id=$appid&redirect_uri=http://qnm.upslope.cn/pay.php&uid=$uid&kbsh_id=$kbsh_id";
        $url = "http://qnm.upslope.cn/home/index/authorization?uid=$uid&kbsh_id=$kbsh_id";
        $url= urlencode($url);
        //输出二维码
//        QRcode($url,8);

        $this->showSuccess('',['url' => $url,'alipay' => $kbsh['alipay']]);
    }

    /**
     * 码商商户列表
     */
    public function index(){
        //status 授权状态  use_status 使用状态
        $name = I('get.name',''); //商户名称
        $use_status = I('get.use_status',9); //使用状态

        $condition = array();
        $condition['uid'] = array('eq',$this->uid);
        if($name){
            $condition['name'] = array("like","%$name%");
        }

        if($use_status != 9){
            //不是全部
            $condition['use_status'] = array('eq',$use_status);
        }

        $one = D('Kbxx')->where(['uid' => $this->uid])->find();
//        $info = D('Kbsh')->where(['uid' => $this->uid])->order('create_time desc')->select();

        $list = $this->lists('Kbsh',$condition);

        if(!$list){
            $kbsh = array();
        }else{
            $kbsh = $list;
        }
        $this->showSuccess('',[
            'kbxx' => $one, //码主信息
            'kbsh' => $kbsh //码商信息
        ]);

    }

    public function detail(){
        $id = I('get.id');
        $info = M('kbsh')->where('id='.$id)->find();
        $this->showSuccess('',$info);
    }

}